|
|
|
|
|
|
|
structure describing the array. The SAFEARRAY structure contains a pointer to the array data. |
|
|
|
|
|
|
|
|
Figure S7-2:
Array parameters are passed as pointers
to pointers to a SAFEARRAY |
|
|
|
|
|
|
|
|
When you declare an API parameter as an array, Visual Basic passes a pointer to the array variablea pointer to a pointer to a SAFEARRAY structure. |
|
|
|
|
|
|
|
|
Now this is all very interesting, but what does it have to do with passing an array to the Polyline function? Nothing! The only API functions you will ever see that can work with SAFEARRAY structures are those that deal with the OLE API. The point of this discussion is that you should avoid declaring parameters as arrays when API functions specify pointers to arrays. |
|
|
|
|
|
|
|
|
But how, then, can you pass a pointer to an array? Look again at Figure S7-1. An array, from the Polygon function's perspective, can be thought of as a pointer to the first element in an array. As long as all of the structures are contiguous in memory, all you need to do is make sure that the address of the first structure in the array is passed as a parameter. This suggests the following declaration: |
|
|
|
|
|
|
|
|
Private Declare Function Polyline Lib "gdi32" (ByVal hdc As Long, _
lppt As POINTAPI, ByVal nCount As Long) As Long |
|
|
|
|
|
|
|
|
Now you're passing a single POINTAPI structureor are you? Since the parameter is passed by reference, you are passing the address of that structure. As long as it is followed in memory by the other structures in the array, you are effectively passing a pointer to the entire arrayexactly the scenario shown in Figure S7-1. |
|
|
|
|
|
|
|
|
But this change also requires a change in the way the function is called. If you just change the declaration, you will get a ByRef argument mismatch error. The function call must be changed from |
|
|
|
|
|
|
|
|
Call Polyline(hdc, PointArray(), points) |
|
|
|
|
|
|
|
|
Call Polyline(hdc, PointArray(0), points) |
|
|
|
|
|
|
|
|
You must pass the first element in the array rather than trying to pass the entire array. |
|
|
|
|
|
|
|
|
That's all you need to do to fix the program. |
|
|
|
|
|